from django.db import models

# Create your models here.


class Category(models.Model):
    name = models.CharField(max_length=20, unique=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "类别"
        verbose_name_plural = verbose_name
        ordering = ['id']


class Goods(models.Model):
    name = models.CharField(max_length=100, verbose_name='商品名称')
    desc = models.CharField(max_length=100, verbose_name='商品描述')
    price = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='现价')
    oldprice = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='原价')
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

    def getImg(self):
        return self.inventory_set.first().color.value

    def getColors(self):
        colors = []
        for inventory in self.inventory_set.all():
            color = inventory.color
            if color not in colors:
                colors.append(color)
        return colors

    def getSizes(self):
        sizes = []
        for inventory in self.inventory_set.all():
            size = inventory.size
            if size not in sizes:
                sizes.append(size)
        return sizes

    def getDetails(self):
        import collections
        datas = collections.OrderedDict()

        for detail in self.goodsdetailsitem_set.all():
            dname = detail.name()
            if dname not in datas:
                datas[dname] = [detail.values]
            else:
                datas[dname].append(detail.values)
        return datas

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Color(models.Model):
    name = models.CharField(max_length=20, verbose_name='颜色名称')
    value = models.ImageField(upload_to='media/color', verbose_name='图片地址')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "商品颜色"
        verbose_name_plural = verbose_name
        ordering = ['id']


class Size(models.Model):
    name = models.CharField(max_length=20, verbose_name='尺寸名称')
    value = models.CharField(max_length=20, verbose_name='尺寸值')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品尺寸'
        verbose_name_plural = verbose_name
        ordering = ['id']


class GoodsDetails(models.Model):
    name = models.CharField(max_length=20, verbose_name='商品详情名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品详情'
        verbose_name_plural = verbose_name
        ordering = ['id']


class GoodsDetailsItem(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    goodsdetails = models.ForeignKey(GoodsDetails, on_delete=models.CASCADE, verbose_name='商品详情')
    values = models.ImageField(upload_to='media', verbose_name='图片地址')

    def __str__(self):
        return self.values

    def name(self):
        return self.goodsdetails.name

    class Meta:
        verbose_name = '商品详情明细'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Inventory(models.Model):
    color = models.ForeignKey(Color, on_delete=models.CASCADE, verbose_name='商品颜色')
    size = models.ForeignKey(Size, on_delete=models.CASCADE, verbose_name='商品尺寸')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    count = models.IntegerField(default=100, verbose_name='数量')

    def __str__(self):
        return self.count

    class Meta:
        verbose_name = '商品库存'
        verbose_name_plural = verbose_name
        ordering = ['id']
